package com.sduept.nwld.dataserver.rest;

import com.sduept.bigdata.weather.entity.IceMonitor;
import com.sduept.bigdata.weather.entity.SimpleIcingModel;
import com.sduept.bigdata.weather.manager.IceMonitorGenManager;
import com.sduept.bigdata.weather.manager.IceMonitorManager;
import com.sduept.bigdata.weather.manager.WeatherCache;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.faultforecast.FaultForecastService;
import com.sduept.nwld.dataserver.manager.faultforecast.IceFaultForecastSampleGenManager;
import com.sduept.nwld.dataserver.manager.faultforecast.IceFaultForecastSampleManager;
import com.sduept.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用于覆冰预测时的测试
 */
@Slf4j
@RestController
@RequestMapping("/iceFault")
public class IceFaultForecastTestService {
	@Autowired
	private IceFaultForecastSampleGenManager genManager;
	@Autowired
	@Qualifier("IceFaultForecastImpl")
	private FaultForecastService service;
	@Autowired
	private IceMonitorGenManager imgm;
	@Autowired
	private WeatherCache wc;
	@Autowired
	private IceMonitorManager imm;
	@Autowired
	private SuperDAO dao;
	@Autowired
	private IceFaultForecastSampleManager sampleManager;


	@GetMapping("/genSamples")
	public String genSamples() { 
		//TODO 解决开头的时候，因为时间相同导致的问题 
		Date startTime = DateUtil.getDateByString("2014-01-01 00:00:00", DateUtil.pattern_ymdhms);
		Date endTime = DateUtil.getDateByString("2018-09-05 00:00:00", DateUtil.pattern_ymdhms);
		Date currentTime = new Date(startTime.getTime());
		Calendar s = Calendar.getInstance();
		while(currentTime.before(endTime)) {
			Date eTime = DateUtil.addDays(currentTime, 10);
			if(eTime.after(endTime)) {
				eTime = endTime;
			}
			genManager.genSample(currentTime, eTime);

			Calendar now = Calendar.getInstance();
			long seconds = (now.getTimeInMillis() - s.getTimeInMillis())/1000;
			log.info(DateUtil.getStringDate(currentTime,DateUtil.pattern_ymdhms)
					+"~"+DateUtil.getStringDate(eTime,DateUtil.pattern_ymdhms)+"覆冰样本生成成功"
					+"，共耗时"+seconds+"秒，即"+seconds/60+"分");
			currentTime = eTime;
		}
		Calendar now = Calendar.getInstance();
		long seconds = (now.getTimeInMillis() - s.getTimeInMillis())/1000;
		log.info("全部覆冰样本生成成功"
				+"，共耗时"+seconds/60+"分，即"+seconds/3600+"小时");
		/*List<IceFaultSample> samples = sampleManager.findAll();
		List<FaultRecord> faults = dao.findByCondition(FaultRecord.class, " faultReson = ? ", "覆冰");
		genManager.genFaultInfo(samples, faults);*/
		return "ok";
	}


	@GetMapping("/train")
	public String train() {
		service.startTrain();
		return "1";
	}


	@GetMapping("/forecast")
	public String forecast() {
		SimpleIcingModel iceData = new SimpleIcingModel();
		Map<String, Double> res = service.doForecast(iceData);
		return "1";
	}


	@GetMapping("/genLinePole")
	public String genLinePole() {
		List<IceMonitor> ms = imm.findAll();
		List<IceMonitor> mms = imgm.genLinePoles(ms);
		imm.update(mms);
		return "ok";
	}
}
